package com.caimore.common;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class SmsUtils_sdk {

   private static char[] HexArray = "0123456789ABCDEF".toCharArray();
   public static final byte[] HEX_CHAR_TABLE = new byte[]{(byte)48, (byte)49, (byte)50, (byte)51, (byte)52, (byte)53, (byte)54, (byte)55, (byte)56, (byte)57, (byte)65, (byte)66, (byte)67, (byte)68, (byte)69, (byte)70};


   public static String addFandSwap(String sNum) {
      sNum = "86" + sNum;
      if(sNum.length() % 2 != 0) {
         sNum = sNum + "F";
      }

      char[] cchar = new char[sNum.length()];

      int newNum;
      for(newNum = 0; newNum < sNum.length(); ++newNum) {
         cchar[newNum] = sNum.charAt(newNum);
      }

      for(newNum = 0; newNum < cchar.length - 1; newNum += 2) {
         char temp = cchar[newNum];
         cchar[newNum] = cchar[newNum + 1];
         cchar[newNum + 1] = temp;
      }

      String var4 = new String(cchar);
      return var4;
   }

   public static String delFandSwap(String sNum) {
      sNum = sNum.substring(2);
      char[] cchar = new char[sNum.length()];

      int newNum;
      for(newNum = 0; newNum < sNum.length(); ++newNum) {
         cchar[newNum] = sNum.charAt(newNum);
      }

      for(newNum = 0; newNum < cchar.length - 1; newNum += 2) {
         char temp = cchar[newNum];
         cchar[newNum] = cchar[newNum + 1];
         cchar[newNum + 1] = temp;
      }

      String var4 = new String(cchar);
      var4 = var4.substring(0, var4.length() - 1);
      return var4;
   }

   public static String delFandSwapMU509(String sNum) {
      char[] cchar = new char[sNum.length()];

      int newNum;
      for(newNum = 0; newNum < sNum.length(); ++newNum) {
         cchar[newNum] = sNum.charAt(newNum);
      }

      for(newNum = 0; newNum < cchar.length - 1; newNum += 2) {
         char temp = cchar[newNum];
         cchar[newNum] = cchar[newNum + 1];
         cchar[newNum + 1] = temp;
      }

      String var4 = new String(cchar);
      var4 = var4.substring(0, var4.length() - 1);
      return var4;
   }

   public static int getNowTime() {
      int iNowTime = (int)(System.currentTimeMillis() / 1000L);
      return iNowTime;
   }

   public static long getNowTimeMS() {
      return System.currentTimeMillis();
   }

   public static String getNowTimeString() {
      Date date = new Date();
      return getDateStr(date, "yyyy-MM-dd HH:mm:ss");
   }

   private static String getDateStr(Date date, String formatStr) {
      SimpleDateFormat fomate = new SimpleDateFormat(formatStr);
      return fomate.format(date);
   }

   public static String getNowDateString() {
      Date date = new Date();
      return getDateStr(date, "yyyyMMdd");
   }

   public static String getNowYMString() {
      Date date = new Date();
      return getDateStr(date, "yyyyMM");
   }

   public static String getReceivedPhoneNo(String sCMGL) {
      String CMGL = sCMGL.substring(24, 38);
      CMGL = delFandSwap(CMGL);
      return CMGL;
   }

   public static String getReceivedPhoneNoMU509(String sCMGL) {
      String CMGL = sCMGL.substring(24, 36);
      CMGL = delFandSwapMU509(CMGL);
      return CMGL;
   }

   public static String getReceivedPhoneNo8332(String sCMGL) {
      String CMGL = sCMGL.substring(4, 26);
      String stemp = hexString2intString(CMGL);
      return stemp;
   }

   public static String getReceivedTime(String sCMGL) {
      String CMGL = sCMGL.substring(42, 56);
      char[] cchar = new char[CMGL.length()];

      int newCMGL;
      for(newCMGL = 0; newCMGL < CMGL.length(); ++newCMGL) {
         cchar[newCMGL] = CMGL.charAt(newCMGL);
      }

      for(newCMGL = 0; newCMGL < cchar.length - 1; newCMGL += 2) {
         char temp = cchar[newCMGL];
         cchar[newCMGL] = cchar[newCMGL + 1];
         cchar[newCMGL + 1] = temp;
      }

      String var5 = new String(cchar);
      return "20" + var5;
   }

   public static String getReceivedTimeMU509(String sCMGL) {
      String CMGL = sCMGL.substring(40, 52);
      char[] cchar = new char[CMGL.length()];

      int newCMGL;
      for(newCMGL = 0; newCMGL < CMGL.length(); ++newCMGL) {
         cchar[newCMGL] = CMGL.charAt(newCMGL);
      }

      for(newCMGL = 0; newCMGL < cchar.length - 1; newCMGL += 2) {
         char temp = cchar[newCMGL];
         cchar[newCMGL] = cchar[newCMGL + 1];
         cchar[newCMGL + 1] = temp;
      }

      String var5 = new String(cchar);
      return "20" + var5;
   }

   public static String getReceivedTime8332(String sCMGL) {
      String CMGL = sCMGL.substring(28, 40);
      return "20" + CMGL;
   }

   public static String getSMSMessageContent(String sCMGL) {
      String sPDU7bit = sCMGL.substring(40, 42);
      System.out.println("sPDU7bit=" + sPDU7bit);
      String sLen = sCMGL.substring(56, 58);
      int iLen = Integer.parseInt(sLen, 16);
      String CMGL = sCMGL.substring(58, sCMGL.length());
      if(sPDU7bit.equals("00")) {
         CMGL = decode7bit(CMGL);
      } else if(sPDU7bit.equals("08")) {
         CMGL = decodeUCS2(CMGL);
      }

      return CMGL;
   }

   public static String getSMSMessageContentEC200N(String sCMGL) {
      int index0008 = sCMGL.indexOf("0008");
      int index0000 = sCMGL.indexOf("0000");
      String CMGL;
      if(index0008 > 0) {
         CMGL = sCMGL.substring(index0008 + 20, sCMGL.length());
         CMGL = decodeUCS2(CMGL);
      } else if(index0000 > 0) {
         CMGL = sCMGL.substring(index0008 + 20, sCMGL.length());
         CMGL = decode7bit(CMGL);
      } else {
         CMGL = sCMGL.substring(56, sCMGL.length());
      }

      return CMGL;
   }

   public static String getEC200N7bitorUCS2(String sCMGL) {
      int index0008 = sCMGL.indexOf("0008");
      int index0000 = sCMGL.indexOf("0000");
      String ucs2 = "other";
      if(index0008 > 0) {
         ucs2 = "UCS2";
         System.out.println("sPDU7bit=UCS2");
      } else if(index0000 > 0) {
         ucs2 = "7bit";
         System.out.println("sPDU7bit=7bit");
      } else {
         ucs2 = "other";
         System.out.println("sPDU7bit=other");
      }

      return ucs2;
   }

   public static String getSMSMessageContentMU509(String sCMGL) {
      String sPDU7bit = sCMGL.substring(38, 40);
      String sLen = sCMGL.substring(54, 56);
      int iLen = Integer.parseInt(sLen, 16);
      String CMGL = sCMGL.substring(56, 56 + iLen * 2);
      if(sPDU7bit.equals("00")) {
         CMGL = decode7bit(CMGL);
      } else if(sPDU7bit.equals("08")) {
         CMGL = decodeUCS2(CMGL);
      }

      return CMGL;
   }

   public static String getSMSMessageContent8332(String sCMGL) {
      String sPDU7bit = sCMGL.substring(42, 44);
      String sLen = sCMGL.substring(56, 58);
      int iLen = Integer.parseInt(sLen, 16);
      String CMGL = sCMGL.substring(58, 58 + iLen * 2);
      if(sPDU7bit.equals("01")) {
         CMGL = hexString2intString(CMGL);
      } else if(sPDU7bit.equals("06")) {
         CMGL = decodeUCS2(CMGL);
      }

      return CMGL;
   }

   public static String decode7bit(String src) {
      StringBuilder sb = new StringBuilder();
      if(src != null) {
         int[] bsrc = hexString2ints(src);
         byte left = 0;
         int i = 0;

         for(int j = 0; i < bsrc.length; ++i) {
            byte value = (byte)((bsrc[i] << j | left) & 127);
            sb.append((char)value);
            left = (byte)(bsrc[i] >>> 7 - j);
            ++j;
            if(j == 7) {
               value = (byte)left;
               sb.append((char)value);
               j = 0;
               left = 0;
            }
         }
      }

      return sb.toString();
   }

   public static String encode7bit(String src) {
      String result = null;
      String hex = null;
      if(src != null) {
         result = "";
         byte left = 0;
         byte[] bsrc = src.getBytes();
         int i = 0;

         for(boolean j = false; i < bsrc.length; ++i) {
            int var8 = i & 7;
            if(var8 == 0) {
               left = bsrc[i];
            } else {
               byte value = (byte)(bsrc[i] << 8 - var8 | left);
               left = (byte)(bsrc[i] >>> var8);
               hex = byte2hex(value);
               result = result + hex;
               if(i == bsrc.length - 1) {
                  result = result + byte2hex(left);
               }
            }
         }

         result = byte2hex((byte)src.length()) + result;
         result = result.toUpperCase();
      }

      return result;
   }

   public static String encode7bit_005100(String src) {
      String result = null;
      String hex = null;
      if(src != null) {
         result = "";
         byte left = 0;
         byte[] bsrc = src.getBytes();
         int i = 0;

         for(boolean j = false; i < bsrc.length; ++i) {
            int var8 = i & 7;
            if(var8 == 0) {
               left = bsrc[i];
            } else {
               byte value = (byte)(bsrc[i] << 8 - var8 | left);
               left = (byte)(bsrc[i] >>> var8);
               hex = byte2hex(value);
               result = result + hex;
               if(i == bsrc.length - 1) {
                  result = result + byte2hex(left);
               }
            }
         }

         result = result.toUpperCase();
      }

      return result;
   }

   public static String decodeUCS2(String src) {
      byte[] bytes = new byte[src.length() / 2];

      for(int reValue = 0; reValue < src.length(); reValue += 2) {
         bytes[reValue / 2] = (byte)Integer.parseInt(src.substring(reValue, reValue + 2), 16);
      }

      String reValue1 = "";

      try {
         reValue1 = new String(bytes, "UTF-16BE");
      } catch (UnsupportedEncodingException var7) {
         System.out.println(var7.getMessage());
      } finally {
         Object bytes1 = null;
      }

      return reValue1;
   }

   public static byte[] encodeUCS2(String message) throws UnsupportedEncodingException {
      byte[] textPart = message.getBytes("UTF-16BE");
      byte[] ret = new byte[textPart.length + 1];
      ret[0] = (byte)(textPart.length & 255);
      System.arraycopy(textPart, 0, ret, 1, textPart.length);
      return ret;
   }

   public static byte[] encodeUCS2_005100(String message) throws UnsupportedEncodingException {
      byte[] userData = message.getBytes("UTF-16BE");
      byte[] ret = new byte[userData.length];
      System.arraycopy(userData, 0, ret, 0, userData.length);
      return ret;
   }

   public static byte[] encode8332UCS2(String message) throws UnsupportedEncodingException {
      StringBuffer sb = new StringBuffer();

      for(int src = 0; src < message.length(); ++src) {
         sb.append(message.substring(src, src + 1));
      }

      String var4 = sb.toString();
      byte[] userData = var4.getBytes("UTF-16BE");
      return userData;
   }

   public static byte[] encode8630UCS2(String message) throws UnsupportedEncodingException {
      ArrayList lstByte = new ArrayList();

      for(int userData = 0; userData < message.length(); ++userData) {
         String i = message.substring(userData, userData + 1);
         byte[] byTemps = i.getBytes("UTF-16BE");
         byte[] var8 = byTemps;
         int var7 = byTemps.length;

         for(int var6 = 0; var6 < var7; ++var6) {
            byte byTemp = var8[var6];
            lstByte.add(Byte.valueOf(byTemp));
         }
      }

      byte[] var9 = new byte[lstByte.size()];

      for(int var10 = 0; var10 < lstByte.size(); ++var10) {
         var9[var10] = ((Byte)lstByte.get(var10)).byteValue();
      }

      return var9;
   }

   public static String decode8332UCS2(String src) {
      StringBuffer sb = new StringBuffer();

      for(int SrcSb = 0; SrcSb < src.length(); SrcSb += 2) {
         sb.append('\u0000' + src.substring(SrcSb, SrcSb + 2));
      }

      String SrcSb1 = sb.toString();
      byte[] bytes = new byte[SrcSb1.length() / 2];

      for(int reValue = 0; reValue < SrcSb1.length(); reValue += 2) {
         bytes[reValue / 2] = (byte)Integer.parseInt(SrcSb1.substring(reValue, reValue + 2), 16);
      }

      String reValue1 = "";

      try {
         reValue1 = new String(bytes, "UTF-16BE");
      } catch (UnsupportedEncodingException var6) {
         System.out.println(var6.getMessage());
      }

      return reValue1;
   }

   public static String bytesToHexString(byte[] src) {
      int size = src.length;
      char[] c = new char[size * 2];

      for(int i = 0; i < size; ++i) {
         byte bint = src[i];
         c[i * 2] = HexArray[(bint & 240) >>> 4];
         c[i * 2 + 1] = HexArray[bint & 15];
      }

      return (new String(c)).toUpperCase();
   }

   public static String byte2hex(byte src) {
      char[] c = new char[]{HexArray[(src & 240) >>> 4], HexArray[src & 15]};
      return (new String(c)).toUpperCase();
   }

   public static int hexChar2int(byte hexChar) {
      return hexChar < HEX_CHAR_TABLE[10]?hexChar - HEX_CHAR_TABLE[0]:hexChar - HEX_CHAR_TABLE[10] + 10;
   }

   public static byte[] hexString2Byte(String s) {
      byte[] raw = s.getBytes();
      byte[] hex = new byte[raw.length / 2];

      for(int i = 0; i < hex.length; ++i) {
         int leftBit4 = hexChar2int(raw[2 * i]) << 4;
         hex[i] = (byte)(leftBit4 | hexChar2int(raw[2 * i + 1]));
      }

      return hex;
   }

   public static byte[] hexString2Bytes(String value) {
      byte[] ret = null;

      try {
         int len = value.length() / 2;
         ret = new byte[len];

         for(int i = 0; i < len; ++i) {
            int iTemp = Integer.parseInt(value.substring(i * 2, i * 2 + 2), 16);
            ret[i] = (byte)iTemp;
         }
      } catch (Exception var5) {
         ;
      }

      return ret;
   }

   public static int[] hexString2ints(String value) {
      int[] ret = null;

      try {
         int len = value.length() / 2;
         ret = new int[len];

         for(int i = 0; i < len; ++i) {
            int iTemp = Integer.parseInt(value.substring(i * 2, i * 2 + 2), 16);
            ret[i] = iTemp;
         }
      } catch (Exception var5) {
         ;
      }

      return ret;
   }

   public static String hexString2intString(String s) {
      byte[] baKeyword = new byte[s.length() / 2];

      for(int e1 = 0; e1 < baKeyword.length; ++e1) {
         try {
            baKeyword[e1] = (byte)(255 & Integer.parseInt(s.substring(e1 * 2, e1 * 2 + 2), 16));
         } catch (Exception var5) {
            System.out.println(var5.getMessage());
         }
      }

      try {
         s = new String(baKeyword, "utf-8");
      } catch (Exception var4) {
         System.out.println(var4.getMessage());
      }

      return s;
   }

   public static String sendReceivedNum(String sReceivedNum) {
      String sTempReceivedNum = addFandSwap(sReceivedNum);
      String sReceivedNumLen = Integer.toHexString(sReceivedNum.length() + 2).toUpperCase();
      if(sReceivedNumLen.trim().length() == 1) {
         sReceivedNumLen = "0" + sReceivedNumLen;
      }

      sTempReceivedNum = "5100" + sReceivedNumLen + "91" + sTempReceivedNum;
      return sTempReceivedNum;
   }

   public static String sendReceivedNum8332(String sReceivedNum) {
      String sTempReceivedNum = "";
      String sReceivedNumLen = Integer.toHexString(sReceivedNum.length()).toUpperCase();
      if(sReceivedNumLen.trim().length() == 1) {
         sReceivedNumLen = "0" + sReceivedNumLen;
      }

      sTempReceivedNum = sReceivedNumLen + "\"" + sReceivedNum + "\"";
      return sTempReceivedNum;
   }

   public static String genImageId() {
      long ltime = System.currentTimeMillis();
      SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
      String sDayTime = df.format(Long.valueOf(ltime));
      sDayTime = sDayTime.substring(2, sDayTime.length());
      return sDayTime;
   }
}
